<template>
  <div class="flexContent">
    <div class="searchBox">
      <el-form ref="formRef" class="demo-form-inline" @submit.native.prevent label-position="left" label-width="auto">
        <div class="serFlexBox">
          <div class="inputBox">
            <el-row :gutter="12" style="display: flex;flex-wrap: wrap;">
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="名称">
                  <el-input v-model="sKeywords" clearable style="width: 95%" placeholder="请输入..."
                    @keyup.enter.native="searchFn" />
                </el-form-item>
              </el-col>
            </el-row>
          </div>
          <div class="btnBox">
            <el-button @click.stop="searchFn" class="searchBtn" icon="el-icon-search">搜索</el-button>
            <el-button @click.stop="emptyFn" class="resetBtn" icon="el-icon-refresh-right">重置</el-button>
          </div>
        </div>
      </el-form>
    </div>
    <div class="add">
      <el-button class="addBtn" @click="addFn">新增</el-button>
    </div>
    <div class="table" id="tableBox" v-height>
      <div style="flex:1;">
        <vxe-table :column-config="{ resizable: true }" class="tablebox" border ref="xTable1"
          :row-config="{ isHover: true }" :scroll-y="{ enabled: true, gt: 21 }" style="width: 100%" height="100%"
          align="center">
          <vxe-column type="seq" title="序号" width="80"></vxe-column>
          <vxe-column field="name" title="名称" />
          <vxe-column field="img" title="图片">
            <template v-slot="{ row }">
              <div class="img" style="padding-top: 10px">
                <img style="width: 100px" :src="row.imgUrl" alt="" />
              </div>
            </template>
          </vxe-column>
          <vxe-column field="describes" title="描述" />
          <vxe-column field="" title="是否显示">
            <template v-slot="{ row }">
              {{ row.showStatus == 0 ? "是" : "否" }}
            </template>
          </vxe-column>
          <vxe-column title="操作" fixed="right" width="150">
            <template v-slot="{ row }">
              <el-button type="text" size="big" style="margin-right: 10px" @click="edit(row)">编辑</el-button>
              <el-button type="text" size="big" style="margin-right: 10px; color: red"
                @click="$delFn('轮播图', del, row)">删除</el-button>
            </template>
          </vxe-column>
        </vxe-table>
      </div>
      <div class="paginationBox">
        <el-pagination :current-page="page.pageNum" background :page-sizes="$pageSizes" :page-size="page.pageSize"
          layout="total,->, sizes, prev, pager, next, jumper" :total="page.total" @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>
    </div>
  </div>
</template>

<script>
import { getBannerList, deleteBanner } from "@/api/h5";
export default {
  name: "BannerInfo",
  data() {
    return {
      bannerList: [],
      sKeywords: "",
      isSearch: false,
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      },
    };
  },
  created() {
    this.getBannerList();
  },
  methods: {
    async getBannerList() {
      if (!this.isSearch) {
        this.sKeywords = "";
      }
      const res = await getBannerList({ ...this.page, name: this.sKeywords });
      if (res.code === 200) {
        const data = res.data;
        this.bannerList = data.list;
        this.page.pageSize = data.pageSize;
        this.page.pageNum = data.pageNum;
        this.page.total = data.total;
        console.log("列表", this.bannerList);
        const $table = this.$refs.xTable1
        $table.loadData(this.bannerList);
      } else {
        // this.$message.error(res.msg);
      }
    },
    async searchFn() {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      }
      this.isSearch = true;
      // this.sKeywords = sKeywords;
      this.getBannerList();
    },
    emptyFn() {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };
      this.isSearch = false;
      this.sKeywords = "";
      this.getBannerList();
    },
    addFn() {
      this.$router.push({
        path: "bannerAdd",
        query: {
          routeType: "add",
        },
      });
    },
    async del(row) {
      const res = await deleteBanner({ id: row.id });
      if (res.code === 200) {
        this.$message.success("删除成功");
        if (this.bannerList.length === 1 && this.page.pageNum > 1)
          this.page.pageNum--;
        this.getBannerList();
      } else {
        // this.$message.error(res.message);
      }
    },
    edit(row) {
      this.$router.push({
        path: "bannerEdit",
        query: {
          routeType: "edit",
          id: row.id,
        },
      });
    },
    // 获取条数改变
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.page.pageNum = 1;
      this.getBannerList();
    },
    // 获取页数改变
    handleCurrentChange(val) {
      this.page.pageNum = val;
      this.getBannerList();
    },
  },
};
</script>

<style scoped lang="scss"></style>
